//#include<netinet/ip.h>
//#include<linux/tcp.h>
//#include<linux/udp.h>
//#include<stdlib.h>
#include "common.h"
#include<string.h>

#define MAX_FROM_LEN 260
#define MAX_TO_LEN 2048
#define MAX_CC_LEN 2048
#define MAX_BCC_LEN 2048
#define MAX_FN_LEN 260
#define MAX_PATH_LEN 260
#define MAX_SUBJ_LEN 1000

typedef enum MsgType
{
    Login = 1,
    Logout = 2,
    Text,
    File
}MsgType;


// Define the presentation of a message node in a message list.
typedef struct MsgNode
{
    unsigned  char srcMac[6];
    unsigned  char destMac[6];
    unsigned long int srcIpv4;     // 4 bytes
    unsigned long int destIpv4;    // 4 bytes
    unsigned short int srcPort;    // 2 bytes
    unsigned short int destPort;   // 2 bytes
    struct ofo_list ofo_list[2]; 
    struct MsgNode *next;
    unsigned long int start_seq;
    char *body;
    int body_flag;
    unsigned short int bodyLen;    // 2 bytes
    unsigned short int real_bodyLen;
    char * data;
    int data_flag;
    unsigned short int data_len;
    unsigned short int real_dataLen;
    unsigned short int type;
    // Keep above data sync with PakectInfo struct 26 Bytes
    
    MsgType msgType;
    unsigned short int s_or_r; //0 send,1 recive
    char username[MAX_UN_LEN + 1];
    char passwd[MAX_PW_LEN + 1];
    char *fileName; // IP and port of soure address are used to name a session.
    int fileLen;
    char *filedata;
    int filedataLen;
    char from[MAX_FROM_LEN + 1];
    char to[MAX_TO_LEN + 1];
    char *friends;
    time_t time;
    time_t lasttime;
    char *text;
    char *path;
    char *Id;
    char *content;
    int content_flag;
    int is_complished;
    int is_offline_msg;
    
    int   is_ok_chunked;  
    int   ok_gzip;
    unsigned long int http_seq;
    
    int count; 
    struct MsgNode *prev;
}MsgNode;

typedef struct usr_record
{
    unsigned  char Mac[6];
    char *username;
    char *usrtype;
    struct usr_record *next;
}usr_record;

struct g_entryList
{
    MsgNode * head;
    MsgNode * tail;
    int count;
}g_entryList;


MsgNode * insert_MsgNode(PacketInfo * packetInfo);
MsgNode * find_MsgNode(PacketInfo * packetInfo, int * is_cons);
void del_MsgNode(MsgNode * Node);
int msg_init(void);
int msg_type(PacketInfo *packetinfo);

usr_record *create_user(void);
int insert_user(usr_record *head, unsigned  char srcMac[6], char username[MAX_UN_LEN + 1]);
int del_user(usr_record *head);
usr_record *search_user(usr_record *head, unsigned  char srcMac[6], char *str);

int write_msg_okdata(MsgNode *node, char *data, unsigned int data_len, struct tcphdr *ptcp);
int write_common(MsgNode *node,PacketInfo *packetinfo);

int write_msg(MsgNode *node, PacketInfo *packetinfo, int frame_len, long len);

char *clear_msgcontent_symbol(char *source);
void Msg_Chunked(MsgNode * node);

usr_record *user_record;

char gtalk_id[40];

